<?php

/**
* Class Renderer: renders html tags
*
* This class helps to build scripts which contain no html
*
*/
class Renderer {

/**
* Constructor
*
* Instantiates a new object
*
*/
  function Renderer (){
  }

/**
*
* render function for html attributes
*
* renders the given attributes that they can be used in a html tag. the function returns a string which directly can be used
* to build a tag.
*
* @param hash    $a    contains the attributes as name value pairs
*/
  function joinAttrs ($a) {
    if (is_array($a)) {
      foreach ($a As $n => $v) {
        $str .= ' '.$n.'="'.$v.'"';
      }
    }
    return $str;
  }

/**
*
* Container tag which contains multiple other tags
*
*
*
* @param int     $depth       depth of tag hierarchy
* @param string  $tagname     name of the tag
* @param hash    $a           attributes for table tag
* @param array   $t           tags the container tag contains
* @var   string  $str         string the multainer function will return
* @var   bool    $bothSpaced  whether start and end tag are forwarded by the $space
*/
  function multainer ($depth ,$tagname ,$a ,$t , $bothSpaced = true) {
    $space = '';
    for ($i=0;$i<$depth;$i++) {
      $space .= "\t";
    }
    $str  = "$space<$tagname";
    $str .= $this->joinAttrs($a);
    $str .= ">\n";
    if (is_array($r)) {
      $str .= join('',$r);
    }
    $str .= ($bothSpaced)?$space:'';
    $str .= "</$tagname>\n";
    return $str;
  }

/**
*
* Html table
*
* renders a html tag that contains a html document
*
* @param array   $t   tags the html tag contains (normally head and body)
*/
  function html ($t) {
    return $this->multainer(0,'html','',$t);
  }

/**
*
* head tag
*
* renders a head tag
*
* @param array   $t   tags the head tag contains (normally meta and title and script)
*/
  function head ($t) {
    return $this->multainer(0,'head','',$t);
  }

/**
*
* title tag
*
* renders a the title of the document
*
* @param string   $str   the document title
*/
  function title ($str='') {
    return "\t<title>$str</title>\n";
  }

/**
*
* meta tag
*
* renders a meta tag which contains meta information about the document
*
* @param string   $name      name of the meta information
* @param string   $content   meta information
*/
  function meta ($name,$content) {
    return '<meta name="'.$name.'" content="'.$content.'">'."\n";
  }

/**
*
* meta http-equiv tag
*
* renders a meta http-equiv tag which is used for special kinds of meta information
*
* @param string   $equiv     name of the meta information
* @param string   $content   meta information
*/
  function meta_equiv ($equiv,$content) {
    return '<meta http-equiv="'.$equiv.'" content="'.$content.'">';
  }

  function body ($a=array(),$t=array()) {
    return "<body".$this->joinAttrs($a).">\n$str</body>\n";
  }

/**
*
* Html table
*
* renders a html table
*
* @param hash    $a   attributes for table tag
* @param array   $r   rows the table contains
*/
  function table ($a=array(),$r=array()) {
    return $this->multainer(0,'table',$a,$r);
  }

/**
*
* table row
*
* renders a html table row which is contained by a table
*
* @param hash    $a   attributes for tr tag
* @param array   $f   fields the table row contains
*/
  function tr ($a=array(), $f=array()) {
    return $this->multainer(1,'tr',$a,$f);
  }

/**
*
* table data
*
* renders a html table data field which is contained by a table row
*
* @param hash    $a   attributes for tr tag
* @param array   $c   content the table data field contains
*/
  function td ($a=array(), $c=array()) {
    return $this->multainer(2,'td',$a,$c,false);
  }

/**
*
* table data
*
* renders a html table data field which is contained by a table row
*
* @param hash    $a   attributes for tr tag
* @param array   $c   content the table data field contains
*/
  function form ($a, $c) {
    return $this->multainer(0,'form',$a,$c);
  }

/**
*
* input field
*
* renders a html input field which is contained by a form
*
* @param hash    $a   attributes for input tag
*/
  function input ($a = array()) {
    return '<input'.$this->joinAttrs($a).'>';
  }

}


?>